package com.itheima.controller;


import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserInfoService;
import com.itheima.service.impl.UserInfoServiceImpl;
import com.itheima.utils.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserInfoServlet extends BaseServlet {
      private UserInfoService userService = new UserInfoServiceImpl();

        /*
        * 登陆
        * */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取页面传递的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
/*        System.out.println(MD5Util.md5("123"));*/

        //获取记住密码的状态
        String ck = req.getParameter("ck");


        //调用业务层方法
        UserInfo userInfo = userService.login(username, password);
        if (userInfo!=null) {

            //根据id查询所有的权限
            UserInfo userAll = userService.ShowAll(userInfo.getId());
            //创建一个
            StringBuffer sb = new StringBuffer();
            List<Role> roles = userAll.getRoles();

            for (Role role : roles) {

                List<Permission> permissions = role.getPermissions();

                for (Permission permission : permissions) {

                    sb.append(permission.getUrl());
                    sb.append(",");

                }
            }
            req.getSession().setAttribute("uri",sb.toString());


            Cookie cookieUM = new Cookie("username",username);
            Cookie cookiePwd = new Cookie("pwd",password);
            //判断是否记住密码
            if("1".equals(ck)){

                //设置Cookie的最大存活时间
                cookieUM.setMaxAge(60*60*24*7);
                cookiePwd.setMaxAge(60*60*24*7);
            }else {
                //如果没记住把存活时间设置为0;
                cookieUM.setMaxAge(0);
                cookiePwd.setMaxAge(0);

            }
            //设置路径
            cookieUM.setPath("/");
            cookiePwd.setPath("/");
            //把cookie添加到客户端
            resp.addCookie(cookieUM);
            resp.addCookie(cookiePwd);

            //添加用户到全局作用域
            req.getSession().setAttribute("userInfo", userAll);
            req.getRequestDispatcher("/pages/main.jsp").forward(req,resp);
        }else {
            req.setAttribute("cwu",0);
            req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
        }

    }
    /*
    * 注销
    * */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //注销Session
        req.getSession().invalidate();

        req.setAttribute("logout",0);


        //跳转页面
        req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);

    }

    /*
    * 修改密码
    * */
    protected void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id 和密码
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        //调用业务层方法
        userService.updateById(id , password);

        //注销Session
        req.getSession().invalidate();
        //获取Cookie对象
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            if("pwd".equals(cookie.getName())){
                cookie.setMaxAge(0);
                cookie.setPath("/");
                resp.addCookie(cookie);
            }
        }

        //跳转页面
        resp.sendRedirect(req.getContextPath()+"/user/logout");
    }


}
